package com.alex.util.file;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.regex.Pattern;

/**
 * 文件处理工具类
 *
 * @author alex
 */
public class FileFwsUtils {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private static final String[] OFFICE_POSTFIXS = {".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx"};
    private static final String PREFIX = "aosp";
    private static final int Crcodesize = 500;

    /**
     * 删除文件
     *
     * @param f
     */
    public static boolean deleteFile(File f) {
        if (f.isFile()) {
            f.delete();
        }
        return true;
    }

    public static boolean deleteFile(String filePath) {
        return deleteFile(new File(filePath));
    }

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDir(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        dir.delete();
        return true;
    }

    public static boolean deleteDir(String dir) {
        return deleteDir(new File(dir));
    }

    /**
     * 判断office格式
     *
     * @param suffix
     * @return
     */
    public static boolean checkFileOfficeSuffix(String suffix) {
        ArrayList<String> officeFormats = Lists.newArrayList();
        Collections.addAll(officeFormats, OFFICE_POSTFIXS);
        for (String officeName : officeFormats) {
            if (officeName.equalsIgnoreCase(suffix)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是图片格式
     *
     * @param attachType
     * @return
     */
    public static boolean isPicture(String attachType) {
        String reg = "\\.(?i)(jpg|jpeg|gif|png)";
        return Pattern.matches(reg, attachType);
    }

    /**
     * 将文件转换成数组
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static byte[] getBytesFromFile(String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        byte[] result = getBytesFromFile(is);
        is.close();
        return result;
    }

    /**
     * 将文件转换成数组
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        byte[] result = getBytesFromFile(is);
        is.close();
        return result;
    }

    /**
     * 将文件转换成数组
     *
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] getBytesFromFile(InputStream inputStream) throws IOException {
        byte[] keyhex = null;
        keyhex = new byte[inputStream.available()];
        inputStream.read(keyhex);
        return keyhex;
    }

    /**
     * 创建文件夹
     *
     * @param file
     */
    public static void mkdir(File file) {
        File dir = file.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }
}
